package com.xiaofan.controller;

import com.xiaofan.bean.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * controller层的cookie和session
 * @author : 晓帆
 * @version : 1.0
 **/
@RestController
@RequestMapping("user")
public class CookieAndSession {
    /**
     * cookie的获取
     * @param request 参数
     */
    @RequestMapping("cookie")
    public void cookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName()+"  "+cookie.getValue());
        }
        HttpSession session = request.getSession();
        session.setAttribute("username","zhangsan");
    }

    /**
     * session的获取
     * @param session 参数
     */
    @RequestMapping("session")
    public void session(HttpSession session){
        System.out.println(session);
        System.out.println(session.getAttribute("username"));
    }

    /**
     * javaBean封装Json数据
     * @param user 参数
     * @return User 返回值
     */
    @RequestMapping("json")
    public User json(@RequestBody User user){
        System.out.println(user);
        return user;
    }

    /**
     * Map封装json数据
     * @param user 参数
     * @return User 返回值
     */
    @RequestMapping("json1")
    public Map json1(@RequestBody Map user){
        int a = (int) user.get("a");
        System.out.println(user);
        return user;
    }

    /**
     * Date日期类型转换（使用MVC自带转换器，使用注解的方式）
     * @param date Date日期类型的参数
     * @return User 返回值
     */
    @RequestMapping("date")
    public User typeConversion(@DateTimeFormat(pattern = "yy-MM-dd HH:mm:ss") Date date){
        System.out.println(date);
        return new User();
    }
}
